package gowcage.slidemenu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gowcage.testandroid.R;

import java.util.List;

/**
 * Created by Administrator on 2017/4/14.
 */

public class ListViewAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<String> list;

    public ListViewAdapter(Context context, List<String> list) {
        inflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //加载布局和绑定控件
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_recycler, null);
            holder.tv = (TextView) convertView.findViewById(R.id.item_recycler_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //设置数据
        holder.tv.setText(list.get(position));

        return convertView;
    }

    class ViewHolder {
        TextView tv;
    }
}
